package com.chushouya.order.service.api.impl;

import com.alipay.api.domain.*;
import com.alipay.api.response.AlipayOpenMiniOrderCreateResponse;
import com.alipay.api.response.AlipayOpenMiniOrderQueryResponse;
import com.chushouya.manager.dao.entity.StoreEntity;
import com.chushouya.manager.service.common.AlipayClientCommonService;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.entity.OrderExtendEntity;
import com.chushouya.order.dao.entity.OrderProductEntity;
import com.chushouya.order.dao.repository.OrderExtendRepository;
import com.chushouya.order.dao.repository.OrderProductRepository;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.service.api.AlipayOpenMiniOrderCreateService;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Jsons;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class AlipayOpenMiniOrderCreateServiceImpl implements AlipayOpenMiniOrderCreateService {

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private OrderExtendRepository orderExtendRepository;


    @Resource
    private OrderProductRepository orderProductRepository;

    @Resource
    private AlipayClientCommonService alipayOpenMiniOrderService;






    /**
     * 获取订单信息
     * @param orderId
     */
    private OrderEntity getOrder(Long orderId) {
        OrderEntity orderEntity = orderRepository.selectByPrimaryKey(orderId);
        if (Objects.isNull(orderEntity)) {
            throw new RuntimeException("订单不存在");
        }
        return orderEntity;
    }




    /**
     * 订单额外信息
     * @param orderId
     */
    private OrderExtendEntity getOrderExtend(Long orderId) {
        OrderExtendEntity orderExtendEntity = orderExtendRepository.selectByOrderId(orderId);
        if (Objects.isNull(orderExtendEntity)) {
            throw Ex.business("订单扩展信息不存在");
        }
        return orderExtendEntity;
    }

    /**
     * 获取订单产品信息
     * @param orderId
     */
    private List<OrderProductEntity> getOrderProduct(Long orderId) {
        List<OrderProductEntity> orderProductEntities = orderProductRepository.getProductListByOrderId(orderId);
        if (CollectionUtils.isEmpty(orderProductEntities)) {
            throw Ex.business("订单产品信息不存在");
        }
        return orderProductEntities;
    }

    @Override
    public AlipayOpenMiniOrderCreateResponse createOpenMiniOrder(Long orderId) {
        return null;
    }

    @Override
    public void orderQuery(Long orderId) {
        OrderEntity orderEntity = getOrder(orderId);
        OrderExtendEntity orderExtendEntity = getOrderExtend(orderId);
        AlipayOpenMiniOrderQueryResponse response = alipayOpenMiniOrderService.alipayOpenMiniOrderQuery(orderEntity.getOrderNo(),orderExtendEntity.getOpenId());
        log.info("支付宝小程序订单查询结果:{}", Jsons.toFormatJsonString(response));
    }

    @Override
    public void orderPayPreCheck(Long orderId) {
        OrderEntity orderEntity = getOrder(orderId);
        OrderExtendEntity orderExtendEntity = getOrderExtend(orderId);
        AlipayCommerceRecycleOrderPreCheckModel model = new AlipayCommerceRecycleOrderPreCheckModel();
        model.setSellerId(orderExtendEntity.getOpenId()); // 用户ID
        model.setMerchantOrderNo(orderEntity.getOrderNo());
        log.info("支付宝小程序订单支付预检查请求参数:{}", Jsons.toFormatJsonString(model));
        alipayOpenMiniOrderService.alipayCommerceRecycleOrderPreCheck(model);
    }


    /**
     * 构建订单请求参数
     */
    private AlipayOpenMiniOrderCreateModel buildMiniOrderCreatParams(OrderEntity orderEntity,StoreEntity storeEntity, OrderExtendEntity orderExtendEntity, List<OrderProductEntity> orderProductList) {
        return null;
    }


    /**
     * 构建order_detail信息
     */
    private MiniOrderDetailDTO buildOrderDetail(OrderEntity orderEntity, List<OrderProductEntity> orderProductList) {
       return null;
    }
    /**
     * 门店信息
     */
    private ShopInfoDTO buildShopInfo(StoreEntity storeEntity) {
        ShopInfoDTO shopInfo = new ShopInfoDTO();
        shopInfo.setName(storeEntity.getStoreName());
        shopInfo.setAddress(storeEntity.getAddress());
        return shopInfo;
    }


    /**
     * 构建delivery_detail
     */
    private LogisticsInfoDTO buildDeliveryDetail() {
        final String deliveryType = "INST_STORE_SERVICE";
        LogisticsInfoDTO deliveryDetail = new LogisticsInfoDTO();
        deliveryDetail.setDeliveryType(deliveryType);
//        DeliveryMerchantInfoDTO deliveryMerchantInfo = new DeliveryMerchantInfoDTO();
//        deliveryMerchantInfo.setMerchantPid(merchantEntity.getPid());
//        deliveryMerchantInfo.setDeductOut(merchantEntity.getUserWalletId());
//        deliveryMerchantInfo.setDeductOut(merchantEntity.getPid());
//        deliveryMerchantInfo.setDeductOutType(merchantEntity.getDeductOutType());
//        deliveryMerchantInfo.setDeductOutType("balance");
//        deliveryDetail.setDeliveryMerchantInfo(deliveryMerchantInfo);
        return deliveryDetail;
    }


}
